package service;

import android.database.sqlite.SQLiteDatabase;

import com.example.firstapp.entity.Phone;

import java.util.Objects;

import util.Database;

//通讯录操作业务逻辑
public class PhoneService {

    /**
     * 添加用户手机号
     *
     * @param db    数据库对象
     * @param phone 手机号信息实体对象
     */
    public static void phone_add(SQLiteDatabase db, Phone phone) {
        if (Objects.equals(phone.getName(), "")) {
            phone.setState(false);
            phone.setMessage("请输入用户名");
        } else if (Objects.equals(phone.getPhoneNumber(), "")) {
            phone.setState(false);
            phone.setMessage("请输入手机号");
        } else if (!Objects.equals(phone.getPhoneNumber().length(), 11)) {
            phone.setState(false);
            phone.setMessage("手机号格式有误");
        } else {
            String[][] data = {{"name", phone.getName()}, {"phoneNumber", phone.getPhoneNumber()}};
            if (Database.addDate(db, "phoneNumber", data)) {
                phone.setState(true);
                phone.setMessage("添加成功");
            } else {
                phone.setState(false);
                phone.setMessage("用户手机号已存在");
            }
        }
    }

    /**
     * 删除用户手机号
     *
     * @param db    数据库对象
     * @param phone 手机号信息实体对象
     */
    public static void phone_delete(SQLiteDatabase db, Phone phone) {
        if (Objects.equals(phone.getName(), "")) {
            phone.setState(false);
            phone.setMessage("请输入用户名");
        } else {
            if (Database.deleteDate(db, "phoneNumber",
                    "name=?", new String[]{phone.getName()})) {
                phone.setState(true);
                phone.setMessage("删除成功");
            } else {
                phone.setState(false);
                phone.setMessage("用户不存在");
            }
        }
    }

    public static void phone_update(SQLiteDatabase db, Phone phone) {
        if (Objects.equals(phone.getName(), "")) {
            phone.setState(false);
            phone.setMessage("请输入用户名");
        } else if (Objects.equals(phone.getPhoneNumber(), "")) {
            phone.setState(false);
            phone.setMessage("请输入手机号");
        } else if (!Objects.equals(phone.getPhoneNumber().length(), 11)) {
            phone.setState(false);
            phone.setMessage("手机号格式有误");
        } else {
            String[][] data = {{"name", phone.getName()}, {"phoneNumber", phone.getPhoneNumber()}};
            if (Objects.equals(phone.getPhoneNumber(), Database.select(db, "phoneNumber",
                    "phoneNumber",
                    "name=?", new String[]{phone.getName()}))) {
                phone.setState(false);
                phone.setMessage("未发生改变");
            } else if (Database.updateDate(db, "phoneNumber", data, "name=?",
                    new String[]{phone.getName()})) {
                phone.setState(true);
                phone.setMessage("修改成功");
            } else {
                phone.setState(false);
                phone.setMessage("修改失败");
            }
        }
    }

    public static void phone_select(SQLiteDatabase db, Phone phone) {
        if (Objects.equals(phone.getName(), "")) {
            phone.setState(false);
            phone.setMessage("请输入用户名");
        } else {
            String phoneNumber = Database.select(db, "phoneNumber", "phoneNumber",
                    "name=?", new String[]{phone.getName()});
            if (!Objects.equals(phoneNumber, "")) {
                phone.setPhoneNumber(phoneNumber);
                phone.setState(true);
                phone.setMessage("查询成功");
            } else {
                phone.setState(false);
                phone.setMessage("查询失败");
            }
        }
    }


}
